package com.niguang.schedule;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.niguang.work.api.entity.ReformContent;
import com.niguang.work.api.entity.ReformUser;
import com.niguang.work.api.enums.EnableFlagEnum;
import com.niguang.work.api.enums.FormFlagEnum;
import com.niguang.work.api.enums.ReformRoleTypeEnum;
import com.niguang.work.api.service.ReformContentService;
import com.niguang.work.api.service.ReformUserService;
import com.niguang.work.api.weChatTemplate.service.ZhengGaiWeChatService;
import com.niguang.work.api.weChatTemplate.vo.ZhengGaiApplyVo;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 隐患上报的推送，发送审核人消息
 * 每天8点、12点、16点、20点
 */
@JobHandler("sendApplyTemplate")
@Component
@Slf4j
public class ScheduleSendApply extends IJobHandler {

    @Resource
    private ReformContentService reformContentService;

    @Resource
    private ReformUserService reformUserService;

    @Resource
    private ZhengGaiWeChatService zhengGaiWeChatService;


    @Override
    public ReturnT<String> execute(String param) {
        EntityWrapper<ReformContent> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq(ReformContent.ENABLE_FLAG, EnableFlagEnum.OK_FLAG.getIndex());
        List<ReformContent> reformContentList = reformContentService.selectList(entityWrapper);
        log.info("审核人消息推送开始：{}", reformContentList);
        if (CollectionUtils.isNotEmpty(reformContentList)) {
            EntityWrapper<ReformUser> userEntityWrapper = new EntityWrapper<>();
            userEntityWrapper.eq(ReformUser.ROLE_TYPE, ReformRoleTypeEnum.APPLY_USER.getIndex());
            List<ReformUser> reformUserList = reformUserService.selectList(userEntityWrapper);
            if (CollectionUtils.isNotEmpty(reformUserList)) {
                reformUserList.forEach(p -> {
                    List<ReformContent> collect = reformContentList.parallelStream().filter(e -> p.getSiteId().equals(e.getSiteId())).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(collect)) {
                        List<ReformContent> contentList = collect.parallelStream().filter(p1 ->
                                p1.getFormFlag().equals(FormFlagEnum.QUALITY_DANGER.getIndex())).collect(Collectors.toList());

                        List<ReformContent> contentList1 = collect.parallelStream().filter(p1 ->
                                p1.getFormFlag().equals(FormFlagEnum.SAFE_DANGER.getIndex())).collect(Collectors.toList());

                        if (CollectionUtils.isNotEmpty(contentList)) {
                            ReformContent reformContent = contentList.get(0);
                            ZhengGaiApplyVo zhengGaiApplyVo = new ZhengGaiApplyVo(p.getOpenId(), reformContent.getSiteName(), contentList.size() + "条", reformContent.getFormFlag());
                            zhengGaiWeChatService.sendApply(zhengGaiApplyVo);
                        } else if (CollectionUtils.isNotEmpty(contentList1)) {
                            ReformContent reformContent = contentList1.get(0);
                            ZhengGaiApplyVo zhengGaiApplyVo = new ZhengGaiApplyVo(p.getOpenId(), reformContent.getSiteName(), contentList1.size() + "条", reformContent.getFormFlag());
                            zhengGaiWeChatService.sendApply(zhengGaiApplyVo);
                        }
                    }
                });
            }
            return ReturnT.SUCCESS;
        }
        return ReturnT.FAIL;
    }
}
